package demo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserMapper  {
    public int insert(User user){
        try (Connection conn = DBUtil.getConnection()){
            String aql = "INSERT INTO user(username,password,nickname,age) "+
                         "VALUES(?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(aql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 根据用户名查询用户数据，如果用户不存在则返回null
      * @param username
     * @return
     */
    public User selectByUsername(String username){
        try (Connection conn = DBUtil.getConnection();){
           String sql = "SELECT id,username,password,nickname,age "+
                        "FROM user "+
                        "WHERE username=?";
           PreparedStatement ps = conn.prepareStatement(sql);
           ps.setString(1,username);
           ResultSet rs = ps.executeQuery();
           if (rs.next()){
               User user = new User();
               user.setId(rs.getInt("id"));
               user.setUsername(rs.getString("username"));
               user.setPassword(rs.getString("password"));
               user.setNickname(rs.getString("nickname"));
               user.setAge(rs.getInt("age"));
               return user;
           }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 修改用户信息
     * 根据传入的user对象，修改数据库中密码，昵称，年龄三个信息
     * @param user
     * @return
     */
    public int update(User user){
        try (Connection conn = DBUtil.getConnection()){
            String sql = "UPDATE user "+
                         "SET password=?,nickname=?,age=? "+
                         "WHERE username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getNickname());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());
            int num = ps.executeUpdate();
            return num;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 查询所有用户信息
     * 在User表中每一条数据对应一个User对象
     *
     * @return
     */
    public List<User> selectAll(){
        List<User> users = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection()){
            Statement st = conn.createStatement();
            String sql = "SELECT id,username,password,nickname,age "+
                         "FROM user";

            ResultSet rs = st.executeQuery(sql);
            while(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return users;
    }



}
